/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-modules.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include "Ogre_Gfx.h"

//===========================================================================================
dylab::Ogre_Gfx::MeshManager::MeshManager()
: mMeshMap(true)
{
    mOgreMeshMgr = Ogre::MeshManager::getSingletonPtr();
    DYLAB_ASSERT(mOgreMeshMgr != NULL);
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::MeshManager::~MeshManager()
{
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::IMesh * dylab::Ogre_Gfx::MeshManager::createMesh(const String & _name)
{
    Mesh * mesh = new Mesh(mOgreMeshMgr->createManual(_name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME));
    DYLAB_ASSERT(mesh != NULL);

    mMeshMap.insert(_name, mesh);
    return mesh;
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::IMesh * dylab::Ogre_Gfx::MeshManager::getMesh(const String & _name)
{
    return mMeshMap.get(_name);
}
//-----------------------------------------------------------------------------
const dylab::Ogre_Gfx::IMesh * 
dylab::Ogre_Gfx::MeshManager::getMesh(const String & _name) const
{
    return mMeshMap.get(_name);
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::MeshManager::destroyMesh(const String & _name)
{
    mMeshMap.remove(_name);
    mOgreMeshMgr->remove(_name);
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::IMesh * dylab::Ogre_Gfx::MeshManager::createPlane(const String & _name, 
    const Plane & _plane, const Vector2 & _size, const Vector2 & _segments, const Vector2 & _tiles)
{
    Ogre::Vector3 ogrePlaneNormal = toOgreVector3(_plane.getNormal());
    Mesh * mesh = new Mesh(mOgreMeshMgr->createPlane(_name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
        Ogre::Plane(ogrePlaneNormal, _plane.getDisplacement()), _size.x(), _size.y(), 
        _segments.x(), _segments.y(), true, 1, _tiles.x(), _tiles.y(), ogrePlaneNormal.perpendicular()));

    DYLAB_ASSERT(mesh != NULL);

    mMeshMap.insert(_name, mesh);
    return mesh;
}
//-----------------------------------------------------------------------------